const original = {
    number: 1,
    bool: false,
    string: 'string',
    date: new Date(),
    regex: /regex/,
    array: [1, 2, 3],
    object: {
        nested: 'nested'
    },
    func: function () { console.log('hello'); }
};


function deepClone(obj, map = new WeakMap()){
    if(obj instanceof Date){
        return new Date(obj)
    }
    if(obj instanceof RegExp){
        return new RegExp(obj)
    }
    if(obj === null || typeof obj !== 'object'){
        return obj
    }
    if(map.has(obj)){
        return map.get(obj)
    }
    const Con = obj.constructor
    let res = new Con()
    map.set(obj, res)

    for(let key in obj){
        if(obj.hasOwnProperty(key)){
            res[key] = deepClone(obj[key], map)
        }
    }
    return res;
}
console.log(deepClone(original))


function hasCycle(obj, map = new WeakMap()){
    if(obj === null || typeof obj !== 'object'){
        return false
    }
    if(map.get(obj)){
        return true
    }
    map.set(obj, true)

    for(let key  in obj){
        if(obj.hasOwnProperty(key)){
            if(typeof obj[key] === 'object' && hasCycle(obj[key], map)){
                return true
            }
        }
    }
    return false

}
// 示例  
const aa = {c: 1};  
const bb = { aa };  
aa.bb = bb; // 创建循环依赖  
  
console.log(hasCycle(aa), 'hascycle'); // 输出: true 